package com.leen.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leen.common.vo.ResponseVO;
import com.leen.common.vo.ResultEnum;
import com.leen.common.vo.ResultVO;
import com.leen.news.dto.YxSystemStoreDTO;
import com.leen.news.entity.YxSystemStore;
import com.leen.news.entity.YxSystemStoreStaff;
import com.leen.news.mapper.YxSystemStoreMapper;
import com.leen.news.service.YxSystemStoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class YxSystemStoreServiceImpl implements YxSystemStoreService {
    @Autowired
    private YxSystemStoreMapper yxSystemStoreMapper;

    @Override
    public ResponseVO search(YxSystemStoreDTO yxSystemStoreDTO) {
        IPage<YxSystemStore> page = new Page<>(yxSystemStoreDTO.getPageNumber(), yxSystemStoreDTO.getPageSize());
        QueryWrapper<YxSystemStore> queryWrapper=new QueryWrapper<>();
//        if (StringUtils.isNotBlank(yxSystemStoreDTO.getName())){
//            queryWrapper.like("name",yxSystemStoreDTO.getName());
//        }
        queryWrapper.eq("is_del", "0");
        IPage<YxSystemStore> page1 = this.yxSystemStoreMapper.selectPage(page, queryWrapper);
        return new ResponseVO(page1.getTotal(),page1.getRecords());
    }

    @Override
    public ResultVO<String> insert(YxSystemStore entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        int insert = this.yxSystemStoreMapper.insert(entity);
        return insert > 0 ? ResultVO.success(ResultEnum.DB_SUCCESS.getMessage()) : ResultVO.failed(ResultEnum.DB_FAILED);
    }

    @Override
    public ResultVO<String> updateById(YxSystemStore entity) {
        entity.setUpdateTime(new Date());
        int update = this.yxSystemStoreMapper.updateById(entity);
        return update > 0 ? ResultVO.success(ResultEnum.DB_SUCCESS.getMessage()) : ResultVO.failed(ResultEnum.DB_FAILED);
    }

    @Override
    public ResultVO<YxSystemStore> selectById(Serializable id) {
        YxSystemStore yxSystemStore = this.yxSystemStoreMapper.selectById(id);
        return ResultVO.success(yxSystemStore);
    }

    @Override
    public List<YxSystemStore> findAll() {
        QueryWrapper<YxSystemStore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", "0");
        return this.yxSystemStoreMapper.selectList(queryWrapper);
    }
}
